﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="omandType_v3", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(OmandTypeV3))]
    public class OmandTypeV3Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IOmandTypeV3 clazz;
        
        public OmandTypeV3Serializer(IOmandTypeV3 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("suurus");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.Suurus))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("omandiliik");
            if (clazz.Omandiliik!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Omandiliik))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Omandiliik");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("roll");
            if (clazz.Roll!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Roll))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Roll");
            }
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.Omanikud != null) 
                        && (x < clazz.Omanikud.Count)); x = (x + 1)) {
                if (clazz.Omanikud[x]!=null) {
                    Writer.WriteStartElement("omanikud");
                    global::Xtee.Core.SerializationMetadata metaOmanikud = this.GetMetaData(clazz.Omanikud[x]);
                    Writer.WriteTypeAttribute(metaOmanikud.TypeQualifiedName.Namespace, metaOmanikud.TypeQualifiedName.Name);
                    metaOmanikud.NewSerializer(clazz.Omanikud[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.Liikmesus!=null) {
                Writer.WriteStartElement("liikmesus");
                global::Xtee.Core.SerializationMetadata metaLiikmesus = this.GetMetaData(clazz.Liikmesus);
                Writer.WriteTypeAttribute(metaLiikmesus.TypeQualifiedName.Namespace, metaLiikmesus.TypeQualifiedName.Name);
                metaLiikmesus.NewSerializer(clazz.Liikmesus).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.Kontakt!=null) {
                Writer.WriteStartElement("kontakt");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Kontakt))).ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="suurus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType_v3>suurus");
                }
                clazz.Suurus = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("suurus")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType_v3>suurus", Reader.LocalName);
            }
            if (Reader.LocalName=="omandiliik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType_v3>omandiliik");
                }
                clazz.Omandiliik = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("omandiliik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType_v3>omandiliik", Reader.LocalName);
            }
            if (Reader.LocalName=="roll") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType_v3>roll");
                }
                clazz.Roll = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("roll")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType_v3>roll", Reader.LocalName);
            }
            if (Reader.LocalName=="omanikud") {
                clazz.Omanikud = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "omanikud")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaOmanikud = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>(Reader, "kapitaliOmanikType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3 item = metaOmanikud.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>();
                        clazz.Omanikud.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("omanikud");
                            metaOmanikud.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Omanikud.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="liikmesus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType_v3>liikmesus");
                }
                global::Xtee.Core.SerializationMetadata metaLiikmesus = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IHyLiikmesusTypeV3>(Reader, "hy_liikmesusType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Liikmesus = metaLiikmesus.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IHyLiikmesusTypeV3>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("liikmesus");
                    metaLiikmesus.NewSerializer(clazz.Liikmesus).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="kontakt") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType_v3>kontakt");
                }
                clazz.Kontakt = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kontakt")).Value;
            }
        }
    }
}
